using System;
using System.Collections.Generic;

namespace Kopernikus.Parsing.Utility
{
    public struct Pair<F, S> : IEquatable<Pair<F, S>>
    {
        private static readonly EqualityComparer<F> FC = EqualityComparer<F>.Default;
        private static readonly EqualityComparer<S> SC = EqualityComparer<S>.Default;
        private F m_first;
        private S m_second;

        public Pair(F first, S second)
        {
            m_first = first;
            m_second = second;
        }

        public F First
        {
            get { return m_first; }
        }

        public S Second
        {
            get { return m_second; }
        }

        #region object overrides

        public override string ToString()
        {
            return string.Format("({0},{1})", m_first, m_second);
        }

        public override bool Equals(object obj)
        {
            return Equals((Pair<F, S>) obj);
        }

        public override int GetHashCode()
        {
            return FC.GetHashCode(m_first) ^ SC.GetHashCode(m_second);
        }

        #endregion

        #region IEquatable<Pair<F,S>> Members

        public bool Equals(Pair<F, S> other)
        {
            return
                FC.Equals(m_first, other.m_first) &&
                SC.Equals(m_second, other.m_second);
        }

        #endregion

        public static Pair<F, S> Create(F first, S second)
        {
            Pair<F, S> p;
            p.m_first = first;
            p.m_second = second;
            return p;
        }
    }
}